package com.wangyh.edit.prettyedit.tree;

import com.wangyh.edit.prettyedit.constant.EditConstant;
import com.wangyh.edit.prettyedit.mainframe.MainFrameView;
import com.wangyh.edit.prettyedit.util.FileImgUtils;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;

import java.io.File;
import java.io.IOException;

public class MenuContextFile {

    /**
     * 构造文件弹出菜单
     *
     * @param tree
     */
    public static void generateFileMenu(TreeView tree, ContextMenu addMenu){
        MenuItem openDiskMenuItem = new MenuItem("打开文件路径");
        //分割线
        SeparatorMenuItem separator = new SeparatorMenuItem();
        MenuItem deleFileMenuItem = new MenuItem("删除文件");
        MenuItem reloadMenuItem = new MenuItem("重新加载");

        deleFileMenuItem.getStyleClass().add("zkh_MenuItem");
        reloadMenuItem.getStyleClass().add("zkh_MenuItem");
        openDiskMenuItem.getStyleClass().add("zkh_MenuItem");

        addMenu.getItems().addAll(openDiskMenuItem, separator,deleFileMenuItem,reloadMenuItem);

        //我的电脑打开
        openDiskMenuItem.setOnAction((ActionEvent t) -> {
            TreeItem<File> selectItem = (TreeItem) tree.getSelectionModel().getSelectedItem();
            String path = selectItem.getValue().getAbsolutePath();

            try {
                Runtime.getRuntime().exec("EXPLORER.EXE /e,/select," + path);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });

        //删除文件
        deleFileMenuItem.setOnAction((ActionEvent t) -> {
            TreeItem<File> selectItem = (TreeItem) tree.getSelectionModel().getSelectedItem();
            String path = selectItem.getValue().getAbsolutePath();

            //判断是否存在
            File f = new File(path);
            if(!f.exists()){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText(null);
                alert.setTitle("提示！");
                alert.setHeaderText("当前结点不存在！");
                alert.show();
                return;
            }
            if(f.isDirectory()){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText(null);
                alert.setTitle("提示！");
                alert.setHeaderText("当前结点不是文件夹");
                alert.show();
                return;
            }

            //删除文件夹
            f.delete();

            tree.requestFocus();
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    boolean remove = selectItem.getParent().getChildren().remove(selectItem);
                }
            });
        });

        //重新加载
        reloadMenuItem.setOnAction((ActionEvent t) -> {
//            tree.refresh();
//            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
//            //左侧树
//            LeftTreeView leftTreeView = new LeftTreeView();
//            TreeView newTree = leftTreeView.getTree();
//            borderPane.setLeft(newTree);

            //更新父节点下面的子节点
            TreeItem<File> parent = ((TreeItem<File>)tree.getSelectionModel().getSelectedItem()).getParent();
            tree.getSelectionModel().select(parent);
            parent.getChildren().clear();
            //更新儿子
            for(final File sonfile: parent.getValue().listFiles()){
                if(sonfile.isDirectory()){
                    ImageView folderIcon = new ImageView(
                            new Image(MainFrameView.class.getResourceAsStream("/img/folder.png"))
                    );
                    folderIcon.setFitHeight(EditConstant.treeIconHeight);
                    folderIcon.setFitWidth(EditConstant.treeIconWeight);
                    parent.getChildren().add(new FileTreeItem(sonfile,folderIcon));
                }else{
                    //设置图标
                    ImageView fileIcon = FileImgUtils.getFileImageView(sonfile);
                    parent.getChildren().add(new FileTreeItem(sonfile,fileIcon));
                }
            }
            //复制完，该节点需要展开。
            parent.setExpanded(true);
        });
    }
}
